
var productId=getQuery("productId");

window.onload=function (){
    if (productId !=null && productId !=''){
        buy();
    }
    showShop();
}

/**
 * 显示购物车信息
 */
function showShop(){
    axios.get("/custom/shop/findByUser",null).then(e =>{
        var info=e.data;
        var str="";
        var total=0;
        for (i=0;i<info.length;i++){
            var obj=info[i];

            str +=`<tr><td>${obj.product.name}</td><td>${obj.product.price}</td><td>${obj.product.madeIn}</td>
                            <td><input type="text" value="${obj.num}" onblur="updateNum(${obj.id},this)"></td><td>${obj.product.price * obj.num}</td>
                            <td><a href="javascript:del(${obj.id})">移除商品</a></td></tr>`
            total += obj.product.price * obj.num ;
        }
        $("shopDate").innerHTML=str;
        $("totalSpan").innerHTML=total;
    })

}



function  del(shopId){
    axios.get("/custom/shop/del",{
        params: {
            shopId
        }
    }).then(e =>{
        if (e.data=="ok"){
            showShop();
        }
    })
}




 async function  buy(){
    await  axios.get("/custom/shop/buy",{
        params:{
            productId
        }
    }).then(e =>{
        if (e.data=="ok"){
            showShop();
        }
    })

}

/**
 * 修改商品购买数量
 * @param shopId
 * @param textObj
 */
function  updateNum(shopId,textObj){
    axios.get("/custom/shop/updateNum",{
        params:{
            shopId,
            newNum:textObj.value
        }
    }).then(e =>{
        if (e.data != "ok"){
            alert("购物车更新失败")
        }
        else {
            showShop()
        }

    })


}

function addOrder(){
    location.href="/custom/addorder.html?totalPrice="+$("totalSpan").innerHTML
}